package com.RLFZ.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.NocvNews;
import com.RLFZ.service.NocvNewsService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.NewsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 服务控制器
 *
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/news")
public class NocvNewsController {
    @Autowired
    private NocvNewsService nocvNewsService;

    @RequestMapping("/toNews")
    public String toNews(){
        return "news/news";
    }
    /**
     * 查询所有角色带有分页，带有条件查询
     */
    @RequestMapping("/listNews")
    @ResponseBody
    public DataView listNews(NewsVo newsVo){
        QueryWrapper<NocvNews> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(newsVo.getTitle()),"title",newsVo.getTitle());
        IPage<NocvNews> ipage=new Page<>(newsVo.getPage(),newsVo.getLimit());
        nocvNewsService.page(ipage,queryWrapper);
        return new DataView(ipage.getTotal(),ipage.getRecords());
    }

    @RequestMapping("/addOrUpdateNews")
    @ResponseBody
    public DataView addOrUpdateNews(NocvNews nocvNews){
        DataView dataView=new DataView();
        nocvNews.setCreateTime(new Date());
        boolean save = nocvNewsService.saveOrUpdate(nocvNews);
        dataView.setMsg("修改或添加成功");
        dataView.setCode(200);
        return dataView;
    }

    /**
     * 删除角色
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public DataView deleteById(Integer id){
        nocvNewsService.removeById(id);
        DataView dataView=new DataView();
        dataView.setCode(200);
        dataView.setMsg("删成功！");
        return dataView;
    }
}